package com.winit.openapi.ums.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;

import com.alibaba.fastjson.JSON;
import com.winit.common.spi.context.CommandContext;
import com.winit.openapi.controller.BaseController;
import com.winit.openapi.model.RequestMsg;
import com.winit.openapi.model.ResponseMsg;
import com.winit.ums.spi.UmsUserService;
import com.winit.ums.spi.commnad.user.AddChildAccountCommand;
import com.winit.ums.spi.commnad.user.IsExistsEmailCommand;
import com.winit.ums.spi.commnad.user.PasswordCommand;
import com.winit.ums.spi.commnad.user.QueryChildAccountCommand;
import com.winit.ums.spi.commnad.user.QuerySingleChildAccountCommand;
import com.winit.ums.spi.commnad.user.UpdateChildAccountCommand;
import com.winit.ums.spi.vo.user.UserVO;

@Controller
@RequestMapping(value = "/ums/user")
public class UmsUserController extends BaseController {

    @Resource
    private UmsUserService umsUserService;

    /**
     * 校验用户名是否已存在
     * 
     * @param requestMsg
     * @param responseMsg
     * @return
     */
    @RequestMapping(value = "/validateLoginName")
    public String validateLoginName(@ModelAttribute RequestMsg requestMsg, @ModelAttribute ResponseMsg responseMsg) {
        IsExistsEmailCommand command = JSON.parseObject(requestMsg.getData().toString(), IsExistsEmailCommand.class);
        boolean isExists = umsUserService.isExistsEmail(command);
        Map<String, String> data = new HashMap<String, String>();
        data.put("result", isExists ? "Y" : "N");
        responseMsg.setData(data);
        return SUCCESS;
    }

    /**
     * 修改用户密码
     * 
     * @param requestMsg
     * @param responseMsg
     * @return
     */
    @RequestMapping(value = "/modifyPassword")
    public String modifyPassword(@ModelAttribute RequestMsg requestMsg, @ModelAttribute ResponseMsg responseMsg) {
        PasswordCommand command = JSON.parseObject(requestMsg.getData().toString(), PasswordCommand.class);
        command.setUsername(command.getCtx().getUsername());
        umsUserService.modifyPassword(command);
        return SUCCESS;
    }
    
    /**
     * 
     * 添加子账号
     * @param requestMsg
     * @param responseMsg
     * @return
     */
    @RequestMapping(value = "/addChildAccount")
    public String addChildAccount(@ModelAttribute RequestMsg requestMsg, @ModelAttribute ResponseMsg responseMsg){
        AddChildAccountCommand command = JSON.parseObject(requestMsg.getData().toString(), AddChildAccountCommand.class);
        command.setCurrentUserName(CommandContext.getContext().getUsername());
        Long childId = umsUserService.addChildAccount(command);         
        Map<String, Object> data = new HashMap<String, Object>();
        data.put("childId",childId);
        responseMsg.setData(data);
        return SUCCESS;
    }
    
    /**
     * 
     * 编辑，停用启用删除子账号
     * @param requestMsg
     * @param responseMsg
     * @return
     */
    @RequestMapping(value = "/updateChildAccount")
    public String updateChildAccount(@ModelAttribute RequestMsg requestMsg, @ModelAttribute ResponseMsg responseMsg){
        UpdateChildAccountCommand command = JSON.parseObject(requestMsg.getData().toString(), UpdateChildAccountCommand.class);
        umsUserService.updateChildAccount(command);       
        return SUCCESS;
    }
    
    /**
     * 查询子账号列表
     * @param requestMsg
     * @param responseMsg
     * @return
     */
    @RequestMapping(value = "/queryChildAccountList")
    public String queryChildAccount(@ModelAttribute RequestMsg requestMsg, @ModelAttribute ResponseMsg responseMsg){
        QueryChildAccountCommand command = JSON.parseObject(requestMsg.getData().toString(), QueryChildAccountCommand.class);
        command.setUserName(CommandContext.getContext().getUsername());
        List<UserVO> userVOs =  umsUserService.queryChildAccountList(command);
        Map<String, Object> data = new HashMap<String, Object>();
        data.put("userVOs",userVOs);
        responseMsg.setData(data);
        return SUCCESS;
    }
    
    /**
     * 
     * 查询单个子账号
     * @param requestMsg
     * @param responseMsg
     * @return
     */
    @RequestMapping(value = "/querySigleChildAccount")
    public String querySigleChildAccount(@ModelAttribute RequestMsg requestMsg, @ModelAttribute ResponseMsg responseMsg){
        QuerySingleChildAccountCommand command = JSON.parseObject(requestMsg.getData().toString(), QuerySingleChildAccountCommand.class);        
        UserVO userVO =  umsUserService.querySigleChildAccount(command);
        Map<String, Object> data = new HashMap<String, Object>();
        data.put("userVO",userVO);
        responseMsg.setData(data);
        return SUCCESS; 
    }

}
